package com.nanyang.tms.api.dto.request.product.update;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiRequest;
import com.nanyang.common.core.exception.ValidateException;
import com.nanyang.tms.api.dto.request.product.add.ProductCarryRuleAddRequest;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Data
public class ProductBaseUpdateRequest extends ApiRequest {

    @JsonIgnore
    private String pin;
    @Valid
    private ProductUpdateRequest productUpdateRequest;
    @Valid
    private PruductRuleUpdateRequest pruductRuleUpdateRequest;
    @Valid
    private List<ProductCarryRuleUpdateRequest> productCarryRuleUpdateRequestList;
    @Valid
    private ProductWeightModelUpdateRequest productWeightModelUpdateRequest;
    @Valid
    private List<ProductPartUpdateRequest> productPartUpdateRequestList;
    @Override
    public void requestCheck() {
            //1、产品计重规则表 国家、城市、货物类型唯一；国家、城市为空时候设置未ALL，有城市信息 国家信息不能为空
            List<String> checkWeightModelList=new ArrayList<>();
            List<ProductWeightRuleUpdateRequest> productWeightRuleUpdateRequestList = productWeightModelUpdateRequest.getProductWeightRuleUpdateRequestList();
            for (ProductWeightRuleUpdateRequest productWeightRuleUpdateRequest : productWeightRuleUpdateRequestList) {
                if(StringUtils.isNotEmpty(productWeightRuleUpdateRequest.getCityCode())&&StringUtils.isEmpty(productWeightRuleUpdateRequest.getCountyCode())){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计重规则中国家信息不能为空！");
                }
                if(StringUtils.isEmpty(productWeightRuleUpdateRequest.getCityCode())){
                    productWeightRuleUpdateRequest.setCityCode("ALL");
                    productWeightRuleUpdateRequest.setCityName("ALL");
                }
                if(StringUtils.isEmpty(productWeightRuleUpdateRequest.getCountyCode())){
                    productWeightRuleUpdateRequest.setCountyCode("ALL");
                    productWeightRuleUpdateRequest.setCountyName("ALL");
                }
                String weightModelStr=productWeightRuleUpdateRequest.getCountyCode()+"-"+productWeightRuleUpdateRequest.getCityCode()+"-"+productWeightRuleUpdateRequest.getCargoTypeCode();
                if(checkWeightModelList.contains(weightModelStr)){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计重规则 国家、城市、货物类型信息唯一，不能存在重复");
                }
                checkWeightModelList.add(weightModelStr);
            }
            //2、计费重进位规则 起始重量和截止重量 区间唯一
            List<String> checkCarryRuleList=new ArrayList<>();
            for (ProductCarryRuleUpdateRequest productCarryRuleUpdateRequest : productCarryRuleUpdateRequestList) {
                String carryRuleStr=productCarryRuleUpdateRequest.getWeightStart()+"-"+productCarryRuleUpdateRequest.getWeightEnd();
                if(checkCarryRuleList.contains(carryRuleStr)){
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "计费重进位规则 起始重量和截止重量区间唯一，不能存在重复");
                }
                checkCarryRuleList.add(carryRuleStr);
            }

    }
}
